
package com.xiaoq;

import java.util.Collections;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.multipart.support.MultipartFilter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

	private static Logger logger = LoggerFactory.getLogger(WebConfig.class);

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		if (logger.isDebugEnabled()) {
			logger.debug("addResourceHandlers, [/static/**, swagger-ui.html, /webjars/**]");
		}
		registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
		registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
		registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
	}

	@Bean
	public FilterRegistrationBean jwtFilter() {
		if (logger.isDebugEnabled()) {
			logger.debug("add jwtTokenFilter, [/api/*]");
		}
		final FilterRegistrationBean registrationBean = new FilterRegistrationBean();
		registrationBean.setUrlPatterns(Collections.singleton("/api/*"));
		registrationBean.setFilter(new JWTTokenAuthenFilter());
		return registrationBean;
	}

	@Bean
	public CommonsMultipartResolver filterMultipartResolver() {
	    return new CommonsMultipartResolver();
	}

	@Bean
	@Order(0)
	public MultipartFilter multipartFilter() {
	    return new MultipartFilter();
	}
}
